const httpStatus = require('http-status');
const ApiError = require('../utils/ApiError');
const catchAsync = require('../utils/catchAsync');
const { SystemSetting } = require('../models');

/**
 * 获取系统设置
 */
const getSystemSettings = catchAsync(async (req, res) => {
  // 系统设置一般只有一条记录，获取第一条
  let systemSettings = await SystemSetting.findOne({ isDeleted: false });
  
  // 如果不存在，则创建默认设置
  if (!systemSettings) {
    systemSettings = await SystemSetting.create({
      systemName: '健康饮食小程序',
      systemVersion: '1.0.0',
      adminEmail: 'admin@example.com',
      contactPhone: '13800138000',
    });
  }
  
  res.send(systemSettings);
});

/**
 * 更新系统设置
 */
const updateSystemSettings = catchAsync(async (req, res) => {
  // 系统设置一般只有一条记录，获取第一条
  let systemSettings = await SystemSetting.findOne({ isDeleted: false });
  
  // 如果不存在，则创建新的记录
  if (!systemSettings) {
    systemSettings = await SystemSetting.create(req.body);
  } else {
    // 如果存在，则更新
    Object.assign(systemSettings, req.body);
    await systemSettings.save();
  }
  
  res.send(systemSettings);
});

module.exports = {
  getSystemSettings,
  updateSystemSettings,
}; 